﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Projekt
{
    public partial class ResultWindow : Form
    {
        private string _content = "";

        public ResultWindow()
        {
            InitializeComponent();
        }

        public void setWebpageTitle ( String title ) 
        {
            this.Text = "Ergebnis: " + title;
        }

        public void setWebpageContent ( String content )
        {
            _content = content;
            resultBox.Text = content;
        }

        
        /// <summary>
        /// Method to resize the ResultBox when window is resized
        /// </summary>
        /// <param name="sender">object - sender</param>
        /// <param name="e">EventArgs - Event arguments</param>
        private void ResultWindow_Resize(object sender, EventArgs e)
        {
            // Get window sizes
            int windowHeight = this.Height;
            int windowWidth = this.Width;

            // Resize ResultBox
            resultBox.Height = windowHeight - 100;
            resultBox.Width = windowWidth - 40;

            // Set Button Positions
            cmdCopyToClipboard.Top = windowHeight - 77;
            cmdCopyToClipboard.Left = windowWidth - 194;

            cmdSaveToFile.Top = windowHeight - 77;
            cmdSaveToFile.Left = windowWidth - 366;

            cmdTokenize.Top = windowHeight - 77;
        }
                
        /// <summary>
        /// Method to copy resultText to Clipboard
        /// </summary>
        /// <param name="sender">object - sender</param>
        /// <param name="e">EventArgs - Event arguments</param>
        private void cmdCopyToClipboard_Click(object sender, EventArgs e)
        {
            // Copy Resulttext in Clipboard
            resultBox.SelectAll();
            resultBox.Copy();

            //Message to user
            MessageBox.Show("Der Text wurde in die Zwischenablage kopiert!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        /// <summary>
        /// Method save resultText to File
        /// </summary>
        /// <param name="sender">object - sender</param>
        /// <param name="e">EventArgs - Event arguments</param>
        private void cmdSaveToFile_Click(object sender, EventArgs e)
        {
            //Stream myStream;
            string sString = _content; //resultBox.Text;

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text File|*.txt";
            saveFileDialog.Title = "Save an Text File";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog.FileName != "")
                {
                    try
                    {
                        resultBox.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.UnicodePlainText);

                        // Message to user
                        MessageBox.Show("Speicherung erfolgreich!" , "Info", MessageBoxButtons.OK,  MessageBoxIcon.Information);
                    }
                    catch (Exception error)
                    {
                        MessageBox.Show("Es ist ein Fehler aufgetreten!\n" + error.Message, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }                    
                }
            }
        }

        private void cmdTokenize_Click(object sender, EventArgs e)
        {

            Tokenizer tokenizer = new Tokenizer();
            string tokenizedString = tokenizer.tokenize(resultBox.Text);
            resultBox.Text = tokenizedString;

        }
    }
}
